using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using KVB.Common;

namespace Customer_Profile.Admin
{
    public class CodeDao
    {
        public CodeDao()
        {
        }

        internal void Add(string Type, string Name, string Value, string Remark, string Orderid, string Used)
        {
            SqlConnection conn = null;
            try
            {                
                string sql = "insert into TP_TCode(Guid,TypeID,CodeName,CodeValue,CodeOrder,Remark,Y_N)values(newid(),N'"+Type+"',N'"+Name+"',N'"+Value+"',"+Orderid+",N'"+Remark+"','"+Used+"')";
                DB.Connection(ref conn);
                DB.ExecuteNonQuery(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CodeDaoAdd", ex.ToString());
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        internal DataSet GetList(string typeId)
        {
            SqlConnection conn = null;
            try
            {
                string sql = "SELECT * FROM TP_TCode where Y_N='Y' and typeid = " + typeId;
                DB.Connection(ref conn);
                return DB.ExecuteDataset(conn, CommandType.Text, sql);                
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CodeDaoGetList", ex.ToString());
                return null;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        public DataSet GetModel(string id)
        {
            SqlConnection conn = null;
            try
            {
                string sql = "select *,(select codeTypeName from TP_TType where id=TP_TCode.typeid) as codetypename from TP_TCode where Guid = '" + id + "'";
                DB.Connection(ref conn);
                return DB.ExecuteDataset(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CodeDaoGetModel", ex.ToString());
                return null;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        internal void Modify(string id,string Name, string Value, string Remark, string Orderid, string Used)
        {
            SqlConnection conn = null;
            try
            {
                string sql = "UPDATE [TP_TCode] SET [CodeName] = N'" + Name + "',[CodeValue] = N'" + Value + "',[CodeOrder] = " + Orderid + ", [Remark] = N'" + Remark + "',[Y_N] = '" + Used + "' WHERE Guid = '" + id + "'";
                DB.Connection(ref conn);
                DB.ExecuteNonQuery(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CodeDaoModify", ex.ToString());
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }
    }
}
